//
//  NewsXMLParser.m
//  Mehfile Zainab
//
//  Created by Sitara Badourali on 03/06/12.
//  Copyright (c) 2012 Mehfile Zainab. All rights reserved.
//

#import "NewsXMLParser.h"
#import "MZNews.h"
#import "URLDataRequester.h"

@implementation NewsXMLParser

@synthesize URLToLoad;

/*
- (NewsXMLParser *) initXMLParser {
    
    [super init];
    
    //appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    return self;
}
*/

- (NewsXMLParser *) initXMLParser:(MZNewsViewController*)aViewController {
    
    [super init];
    
    viewController = aViewController;
    
    currentElementValue = [[NSMutableString alloc]init];
    
    return self;
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {
    
    /*
    if([elementName isEqualToString:@"Books"]) {
        //Initialize the array.
        appDelegate.books = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Book"]) {
        
        //Initialize the book.
        aBook = [[Book alloc] init];
        
        //Extract the attribute here.
        aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];
        
        NSLog(@"Reading id value :%i", aBook.bookID);
    }
    */
    
    if([elementName isEqualToString:@"news"]) {
        //Initialize the array.
        viewController.newsArray = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"current_news"]) {
        
        //Initialize the book.
        news = [[MZNews alloc] init];
        
    }
    
    //NSLog(@"Processing Element: %@", elementName);
    
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    
    /*
    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];
    
    NSLog(@"Processing Value: %@", currentElementValue);
    */
    
    [currentElementValue appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

}



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
    /*
    if([elementName isEqualToString:@"Books"])
        return;
    
    //There is nothing to do if we encounter the Books element here.
    //If we encounter the Book element howevere, we want to add the book object to the array
    // and release the object.
    if([elementName isEqualToString:@"Book"]) {
        [appDelegate.books addObject:aBook];
        
        [aBook release];
        aBook = nil;
    }
    else 
        [aBook setValue:currentElementValue forKey:elementName];
    
    [currentElementValue release];
    currentElementValue = nil;
    */
    
    if([elementName isEqualToString:@"news"])
        return;
    
    //There is nothing to do if we encounter the Books element here.
    //If we encounter the Book element howevere, we want to add the book object to the array
    // and release the object.
    if([elementName isEqualToString:@"current_news"]) {
        [viewController.newsArray addObject:news];
        
        [news release];
        news = nil;
    }
    else {
        //[news setValue:currentElementValue forKey:elementName];
        if([elementName isEqualToString:@"title"]) {
            news.mzNewsTitle = currentElementValue;
        }
        if([elementName isEqualToString:@"content"]) {
            news.mzNewsContent = currentElementValue;
        }   
        if([elementName isEqualToString:@"date"]) {
            news.mzNewsDate = currentElementValue;
        }
        if([elementName isEqualToString:@"image"]) {
            news.mzNewsImage = currentElementValue;
            URLToLoad = currentElementValue;
            NSLog(@"Image to load in cache : %@", URLToLoad);
            //[viewController loadDataWithURL:[NSURL URLWithString:URLToLoad]];
            URLDataRequester *requester = [[URLDataRequester alloc] init];
            requester.viewController = viewController;
            requester.news = news;
            [requester loadDataWithURL:[NSURL URLWithString:URLToLoad]];
        }        
    }
    
    [currentElementValue release];
    //currentElementValue = nil;
    currentElementValue = [[NSMutableString alloc]init];
    
}


- (void) dealloc {
    
    //[aBook release];
    [news release];
    [currentElementValue release];
    [super dealloc];
}

@end